//Page Object
let token = wx.getStorageSync('token');
import { bus } from "../../network/index";
Page({
  data: {
    balance: 0,
    freeze: 0,
    score: 0,
    growth: 0,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    isHide: true,
    userInfo: {},
  },
  goOrder: function(e) {
    wx.navigateTo({
      url: "/pages/order/order?type=" + e.currentTarget.dataset.type
    })
  },
  //options(Object)
  onLoad: function(options) {
    this.getRight();
    console.log(this.data)
  },
  loginout() {
    bus.loginout({
      token
    }).then((res) => {
      console.log(res.data);
      this.getRight()
    }).catch((err) => {
      console.log(err)
    })
  },
  getRight() {
    var that = this;
    //查看是否授权
    wx.getSetting({
      success: function(res) {
        if (res.authSetting['scope.userInfo']) {
          console.log("用户授权了");
          let userInfo = wx.getStorageSync('userInfo');
          that.setData({
            isHide: true,
            userInfo: userInfo
          })
        } else {
          //用户没有授权
          console.log("用户没有授权");
          that.setData({
            isHide: false
          })
        }
      }
    });
  },
  bindGetUserInfo: function(res) {
    if (res.detail.userInfo) {
      //用户按了允许授权按钮
      var that = this;
      // 获取到用户的信息了，打印到控制台上看下
      console.log("用户的信息如下：");
      console.log(res.detail.userInfo);
      wx.setStorageSync('userInfo', res.detail.userInfo)
      //授权成功后,通过改变 isHide 的值，让实现页面显示出来，把授权页面隐藏起来
      that.setData({
        isHide: true,
        userInfo: res.detail.userInfo,
      });

    } else {
      //用户按了拒绝按钮
      wx.showModal({
        title: '警告',
        content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
        showCancel: false,
        confirmText: '返回授权',
        success: function(res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            console.log('用户点击了“返回授权”');
          }
        }
      });
    }
  },
  onReady: function() {

  },
  onShow: function() {
    if (!this.data.isHide) {
      console.log('发货款我哈厉害')
      this.getRight()
    }
  },
  onHide: function() {

  },
  onUnload: function() {

  },
  onPullDownRefresh: function() {

  },
  onReachBottom: function() {

  },
  onShareAppMessage: function() {

  },
  onPageScroll: function() {

  },
  //item(index,pagePath,text)
  onTabItemTap: function(item) {

  }
});